package com.example.mydemo.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mydemo.entity.SysParam;
import com.example.mydemo.entity.Users;
import com.example.mydemo.entity.domain.ResponseDTO;
import com.example.mydemo.mapper.UsersMapper;
import com.example.mydemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping()
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UsersMapper usersMapper;

//    @GetMapping
//    public List<Users> getAllUsers() {
//        return userService.getAllUsers();
//    }

    @PostMapping
    public void createUser(@RequestBody Users user) {
        userService.saveUser(user);
    }

    //通过post命令，将数值写入到数据库
    //通过get命令，将数值写入到数据库
    @PostMapping("/addUser")
    public ResponseDTO<Users> getUsers2(@RequestParam("name") String name , @RequestParam("email") String email,@RequestParam("dep") String dep) {
        Users user = new Users();
        user.setName(name);
        user.setEmail(email);
        user.setDep(Long.valueOf(dep));
        user.setCreateBy("syf");
        user.setCreateTime(new Date());
        userService.saveUser(user);
        return new ResponseDTO<Users>(200, "添加信息成功！", user);
    }
    @DeleteMapping("/delUser")
    public ResponseDTO<Users> delUsers(@RequestParam("id") long id) {
        Users user = new Users();
        user.setIsDelete(1L);
        int updated = usersMapper.update(
                user, // 显式传递实体对象
                new UpdateWrapper<Users>().eq("id", id)
        );
        return new ResponseDTO<Users>(200, "添加信息成功！", user);
    }
    @PostMapping("/update-user")
    public ResponseEntity<Boolean> updateDbParam(
            @RequestParam Long id,
            @RequestParam String name,
            @RequestParam String dep,
            @RequestParam String email
    ) {                Users sysParam = new Users();
        sysParam.setName(name);
        sysParam.setDep(Long.valueOf(dep));
        sysParam.setEmail(email);
        int updated = usersMapper.update(
                sysParam, // 显式传递实体对象
                new UpdateWrapper<Users>().eq("id", id)
        );
        return ResponseEntity.ok(updated > 0);
    }
//    @PutMapping("/{id}")
//    public String updateUser(@PathVariable Long id, @RequestBody Users userDetails) {
//        userService.updateUser(id, userDetails);
//        return "updateSucesss";
//    }

}
